package presentation.view_helpers;

import business.BusinessObjectAssembler;
import business.UserManager;
import business.exception.BusinessException;
import persistence.User;

public class UserBean {

	private User loggedOnUser;
	private UserManager userManager;
	private String userName;
	private String password;
	private Boolean admin;
		
	/** Constructor matching requirement for JavaBeans*/
	public UserBean(){
		userManager = BusinessObjectAssembler.getUserManager();
	}
	/**
	 * Properties
	 */
	public User getLoggedOnUser() {
		return loggedOnUser;
	}
	public void setLoggedOnUser(User currentUser) {
		this.loggedOnUser = currentUser;
	}
	
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	/**
	 * @return String indicating whether login is successful
	 */
	public String login() {
		String retVal="notAuthenticated";
		try {
			this.loggedOnUser = userManager.getUserForCredentials(this.userName, this.password);
			//TODO: handle case, when null is returned
			retVal = "success";
		} catch (BusinessException e) {
			//TODO: create FacesMessage
		}
		return retVal;
	}
	
	public String logout() {
		String retval="logout";
		return retval;
	}
	
	public boolean isAdmin() {
		boolean isAdmin = false;
		try {
			isAdmin = userManager.isAdmin(loggedOnUser);
		} catch (BusinessException e) {
			//TODO: create FacesMessage
		}
		return isAdmin;
	}	
}